package com.zjut.zs.userstatus.manager;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import com.zjut.an.config.dao.AnConfig;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.log.dao.AnLog;
import com.zjut.an.log.manager.AnLogManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.DateUtil;
import com.zjut.an.util.tools.StringUtil;
import com.zjut.zs.applyview.dao.ZsApplyView;
import com.zjut.zs.applyview.manager.ZsApplyViewManager;
import com.zjut.zs.file.dao.ZsFile;
import com.zjut.zs.file.manager.ZsFileManager;
import com.zjut.zs.hjqk.dao.ZsHjqk;
import com.zjut.zs.hjqk.manager.ZsHjqkManager;
import com.zjut.zs.school.dao.ZsSchool;
import com.zjut.zs.school.manager.ZsSchoolManager;
import com.zjut.zs.shhd.dao.ZsShhd;
import com.zjut.zs.shhd.manager.ZsShhdManager;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.userinfo.dao.ZsUserinfo;
import com.zjut.zs.userinfo.manager.ZsUserinfoManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.userstatus.dao.ZsUserStatusDAO;
import com.zjut.zs.zxlb.dao.ZsZxlb;
import com.zjut.zs.zxlb.manager.ZsZxlbManager;


public class ZsUserStatusManager {

	@Resource(name = "ZsUserStatusDAO")
	private ZsUserStatusDAO dao;

	@Resource(name = "DateUtil")
	private DateUtil dateutil;

	@Resource(name = "AnConfigManager")
	private AnConfigManager cmanager;
	
	@Resource(name = "AnLogManager")
	private AnLogManager lomanager;
	
	@Resource(name = "ZsSchoolManager")
	private ZsSchoolManager scmanager;
	
	@Resource(name = "ZsApplyViewManager")
	private ZsApplyViewManager amanager;
	
	@Resource(name = "ZsUserinfoManager")
	private ZsUserinfoManager umanager;
	
	@Resource(name = "StringUtil")
	private StringUtil Stringutil;

	@Resource(name = "AnConfigManager")
	private AnConfigManager anconfig;

	@Resource(name = "ZsFileManager")
	private ZsFileManager fmanager;
	
	@Resource(name = "ZsUserStatusManager")
	private ZsUserStatusManager smanager;
	
	@Resource(name = "ZsZxlbManager")
	private ZsZxlbManager zmanager;
	
	@Resource(name = "ZsShhdManager")
	private ZsShhdManager hmanager;
	
	@Resource(name = "ZsHjqkManager")
	private ZsHjqkManager jmanager;

	public void addData(ZsUserStatus obj) {	
		dao.save(obj);
	}
	
	public void updateData(ZsUserStatus obj) {
		dao.update(obj);
	}
	
	public void deleteData(ZsUserStatus obj) {
		dao.delete(obj);
	}

	public void deleteDatas(List<ZsUserStatus> objlist) {
		for (ZsUserStatus ZsUserStatus : objlist) {
			dao.delete(ZsUserStatus);
		}
	}
	
	public PageModule getDataByInstance (int pageNumber,int pageSize, String sort, String order, ZsUserStatus obj) {
		return dao.findByInstance(pageNumber, pageSize, sort, order, obj);
	}
	
	public int getCountByInstance (ZsUserStatus obj) {
		return dao.countByInstance(obj);
	}
	
	public List getDatas() {
		return dao.findAll();
	}

	public ZsUserStatus getData(String objId) {
		return dao.findById(objId);
	}
	
	public ZsUserStatus getDataByUser(ZsUser user) {
		ZsUserStatus obj = new ZsUserStatus();
		obj.setZsUser(user);
		if(this.getDataByInstance(1, 1, null, null, obj).getObjects().size()>0) {
			return (ZsUserStatus)this.getDataByInstance(1, 1, null, null, obj).getObjects().get(0);
		} else{
			return obj;
		}
	}

	public void appStatus(ZsUserStatus obj, String appStatus) {
		System.out.println("#####"+appStatus);
		if (Status.APPSTATUS_YES.getStatus().equals(appStatus)) {//确认
			obj.setAppStatus(Status.APPSTATUS_YES.getStatus());
		} else if (Status.APPSTATUS_BACK.getStatus().equals(appStatus)) {//申请退回
			obj.setAppStatus(Status.APPSTATUS_BACK.getStatus());
		} else if (Status.APPSTATUS_NULL.getStatus().equals(appStatus)) {//退回
			obj.setAppStatus(Status.APPSTATUS_NULL.getStatus());
		} else if (Status.APPSTATUS_NO.getStatus().equals(appStatus)) {//管理员退回
			obj.setAppStatus(Status.APPSTATUS_NO.getStatus());
		} else if (Status.APPSTATUS_NOTIME.getStatus().equals(appStatus)) {//管理员退回
			obj.setAppStatus(Status.APPSTATUS_NOTIME.getStatus());
		}
		obj.setAppTime(dateutil.getCurrentDate5());
	}
	
	public void sendStatus(ZsUserStatus obj, String sendStatus) {
		System.out.println("#####"+sendStatus);
		if (Status.SENDSTATUS_YES.getStatus().equals(sendStatus)) {//提交
			obj.setSendStatus(Status.SENDSTATUS_YES.getStatus());
			//obj.setAppStatus(Status.APPSTATUS_YES.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"报考申请提交", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} else if (Status.SENDSTATUS_NULL.getStatus().equals(sendStatus)) {
			obj.setSendStatus(Status.SENDSTATUS_NULL.getStatus());
		} else if (Status.SENDSTATUS_BACK.getStatus().equals(sendStatus)) {
			obj.setSendStatus(Status.SENDSTATUS_BACK.getStatus());
			//obj.setAppStatus(Status.SENDSTATUS_BACK.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"报考申请退回", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} else if (Status.SENDSTATUS_NO.getStatus().equals(sendStatus)) {
			
			obj.setAppStatus(Status.APPSTATUS_NULL.getStatus());
			obj.setSendStatus(Status.SENDSTATUS_NO.getStatus());
			obj.setCheckStatus(Status.CHECKSTATUS_NULL.getStatus());//退回并取消初审
			obj.setMessage(obj.getMessage()+"<br>"+dateutil.getCurrentDate5()+"申请被管理员退回，请修改后重新提交");
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"报考申请被管理员退回，请修改后重新提交", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		}
		obj.setSendTime(dateutil.getCurrentDate5());
	}
	
	public void checkStatus(ZsUserStatus obj, String checkStatus) {
		if (Status.CHECKSTATUS_YES.getStatus().equals(checkStatus)) {//初审通过
			obj.setCheckStatus(Status.CHECKSTATUS_YES.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"初审通过", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} else if (Status.CHECKSTATUS_NO.getStatus().equals(checkStatus)) {//初审未过
			obj.setCheckStatus(Status.CHECKSTATUS_NO.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"初审未过", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} else if (Status.CHECKSTATUS_NULL.getStatus().equals(checkStatus)) {//取消初审
			obj.setCheckStatus(Status.CHECKSTATUS_NULL.getStatus());
			obj.setPassStatus(Status.PASSSTATUS_NULL.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"取消初审状态", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		}
		obj.setCheckTime(dateutil.getCurrentDate5());
	}
	
	public void passStatus(ZsUserStatus obj, String passStatus) {
		if (Status.PASSSTATUS_YES.getStatus().equals(passStatus)) {//考试通过
			obj.setPassStatus(Status.PASSSTATUS_YES.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"考试通过", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} else if (Status.PASSSTATUS_NO.getStatus().equals(passStatus)) {//考试未过
			obj.setPassStatus(Status.PASSSTATUS_NO.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"考试未过", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} else if (Status.PASSSTATUS_NULL.getStatus().equals(passStatus)) {//取消考试状态
			obj.setPassStatus(Status.PASSSTATUS_NULL.getStatus());
			//AnLog log = new AnLog(obj.getZsUser().getUsId(), "报考日志","考生"+obj.getUsIdcard()+"取消考试状态", dateutil.getCurrentDate5());
			//lomanager.addData(log);
		} 
		obj.setPassTime(dateutil.getCurrentDate5());
	}
	
	public void setTestNo(List<ZsUserStatus> objlist, Integer begin) {
		String no = cmanager.getSingleConfigInfoByName("准考证编号前缀");
		for (int i=0; i<objlist.size(); i++) {
			ZsUserStatus obj = (ZsUserStatus)objlist.get(i);
			String testNo = no;
			int j = begin+i;
			if (j>99999) {
				testNo = testNo + j;
			} else if (j>9999) {
				testNo = testNo + "0" + j;
			} else if (j>999) {
				testNo = testNo + "00" + j;
			} else if (j>99) {
				testNo = testNo + "000" + j;
			} else if (j>9) {
				testNo = testNo + "0000" + j;
			} else {
				testNo = testNo + "00000" + j;
			}
			obj.setTestNo(testNo);
			dao.update(obj);			
		}
	}
	
	public ZsUserStatus getTestNo(ZsUserStatus obj) {
		try {
			
			if (null==obj.getTestNo() || obj.getTestNo().trim().length()==0) {
				String no = cmanager.getSingleConfigInfoByName("准考证编号前缀");
				synchronized(this) {
					String hql = "FROM ZsUserStatus WHERE testNo=(SELECT MAX(testNo) FROM ZsUserStatus)";
					List objlist = dao.findByHql(1, 1, hql, null);
					if (objlist.size()>0) {
						ZsUserStatus maxobj = (ZsUserStatus)objlist.get(0);
						try {
							int max = Integer.parseInt(maxobj.getTestNo().replace(no, ""));
							obj.setTestNo(no+this.getStringNo(++max));
						} catch (Exception e) {//当前最大值如非数字视为无效
							obj.setTestNo(no+"000001");
						}
					} else {//第一个编号
						obj.setTestNo(no+"000001");
					}
					dao.update(obj);//保存到数据库
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obj;
	}

	/*
	 * public ZsUserinfo getTestNoOrder(ZsUser user) {//分配最新的专业顺序号 ZsUserinfo
	 * userinfo = new ZsUserinfo(); userinfo.setZsUser(user); userinfo =
	 * (ZsUserinfo)umanager.getDataByInstance(1,1,null,null,userinfo).getObjects().
	 * get(0); try { synchronized(this) { ZsApplyView appview =
	 * amanager.getData(user.getUsId()); if (Stringutil.isEmpty(appview.getInSg()))
	 * { String hql =
	 * "select count(exvId) from ZsExValue where column1='"+appview.getColumn1()
	 * +"' and zsUser in (select zsUser from ZsUserStatus where testNo like '%0%')  "
	 * ; System.out.println("#####################"+dao.findCountByHql(hql, null));
	 * int testNoOrder = dao.findCountByHql(hql, null)+1;//该专业已经编号人员数量加一
	 * userinfo.setInSg(String.valueOf(testNoOrder));//保存顺序号。
	 * umanager.updateData(userinfo); } } } catch (Exception e) {
	 * e.printStackTrace(); } return userinfo; }
	 */
	
	public String getStringNo(int j) {
		String testNo = "";
		if (j>99999) {
			testNo = testNo + j;
		} else if (j>9999) {
			testNo = testNo + "0" + j;
		} else if (j>999) {
			testNo = testNo + "00" + j;
		} else if (j>99) {
			testNo = testNo + "000" + j;
		} else if (j>9) {
			testNo = testNo + "0000" + j;
		} else {
			testNo = testNo + "00000" + j;
		}
		return testNo;
	}
	
	public void updateFilesColumn(ZsUser user, Status filetype) {
		System.out.println("####### "+user.getUsId()+" "+filetype.getStatus());
		ZsUserStatus userStatus = new ZsUserStatus();
		userStatus.setZsUser(user);
		userStatus = (ZsUserStatus) smanager.getDataByInstance(1, 1, null, null, userStatus).getObjects().get(0);
		if(Status.ZS_ZXLB.equals(filetype)) {
			StringBuffer zsZxlbs = new StringBuffer();
			List list = zmanager.getDataByUser(user);
			for(int i=0; i<list.size(); i++) {
				ZsZxlb zxlb = (ZsZxlb)list.get(i);
				zsZxlbs.append("["+zxlb.getZxType()+"."+zxlb.getZxName()+"."+zxlb.getZxJb()+"."+zxlb.getZxDate()+"."+zxlb.getZxOrg()+"."+zxlb.getZxDj()+"] ");
			}
			userStatus.setZsZxlb(zsZxlbs.toString());
			smanager.updateData(userStatus);
		} else if(Status.ZS_SHHD.equals(filetype)) {
			StringBuffer zsShhds = new StringBuffer();
			List list = hmanager.getDataByUser(user);
			for(int i=0; i<list.size(); i++) {
				ZsShhd shhd = (ZsShhd)list.get(i);
				zsShhds.append("["+shhd.getHdName()+"."+shhd.getHdJs()+"."+shhd.getHdDateStart()+"-"+shhd.getHdDateEnd()+"."+shhd.getHdOrg()+"] ");
			}
			userStatus.setZsShhd(zsShhds.toString());
			smanager.updateData(userStatus);
		} else if(Status.ZS_HJQK.equals(filetype)) {
			StringBuffer zsHjqks = new StringBuffer();
			List list = jmanager.getDataByUser(user);
			for(int i=0; i<list.size(); i++) {
				ZsHjqk hjqk = (ZsHjqk)list.get(i);
				zsHjqks.append("["+hjqk.getHjName()+"."+hjqk.getHjJb()+"."+hjqk.getHjDate()+"."+hjqk.getHjOrg()+"."+hjqk.getHjDj()+"] ");
			}
			userStatus.setZsHjqk(zsHjqks.toString());
			smanager.updateData(userStatus);
		}		
	}
	
	public void updatefileStatus(ZsUser user) {
		ZsUserStatus userStatus = new ZsUserStatus();
		userStatus.setZsUser(user);
		userStatus = (ZsUserStatus) smanager.getDataByInstance(1, 1, null, null, userStatus).getObjects().get(0);
		ZsSchool school = new ZsSchool();
		school = (ZsSchool)scmanager.getDataByInstance(1,1,null,null,school).getObjects().get(0);
		
		int count = 0;
		String hql = "SELECT COUNT(*) FROM AnConfig WHERE (coClass = '上传材料名称(必需)') AND coValue NOT IN (SELECT  fiName FROM ZsFile WHERE zsUser.usId = "+user.getUsId()+" AND fiType = 'sbcl')";
		count+=dao.findCountByHql(hql,null);
		//System.out.println("==========================" + dao.findCountByHql(hql,null));
		
		if(Status.ON.getStatus().equals(school.getScZxlbActive())) {
			hql = "SELECT COUNT(*) FROM ZsZxlb WHERE zsUser.usId = "+user.getUsId()+" AND zxId NOT IN (SELECT fiName FROM ZsFile WHERE zsUser.usId = "+user.getUsId()+" AND fiType = 'zxlb')";
			count+=dao.findCountByHql(hql,null);
			//System.out.println("==========================" + dao.findCountByHql(hql,null));
		}
		
		if(Status.ON.getStatus().equals(school.getScShhdActive())) {
			hql = "SELECT COUNT(*) FROM ZsShhd WHERE zsUser.usId = "+user.getUsId()+" AND hdId NOT IN (SELECT fiName FROM ZsFile WHERE zsUser.usId = "+user.getUsId()+" AND fiType = 'shhd')";
			count+=dao.findCountByHql(hql,null);
			//System.out.println("==========================" + dao.findCountByHql(hql,null));
		}
		
		if(Status.ON.getStatus().equals(school.getScHjqkActive())) {
			hql = "SELECT COUNT(*) FROM ZsHjqk WHERE zsUser.usId = "+user.getUsId()+" AND hjId NOT IN (SELECT fiName FROM ZsFile WHERE zsUser.usId = "+user.getUsId()+" AND fiType = 'hjqk')";
			count+=dao.findCountByHql(hql,null);
			//System.out.println("==========================" + dao.findCountByHql(hql,null));
		}
		
		if (count == 0) {
			if (!Status.FILESTATUS_YES.getStatus().equals(
					userStatus.getFileStatus())) {
				userStatus.setFileStatus(Status.FILESTATUS_YES.getStatus());
				smanager.updateData(userStatus);
			}
		} else {
			if (!Status.FILESTATUS_NO.getStatus().equals(
					userStatus.getFileStatus())) {
				userStatus.setFileStatus(Status.FILESTATUS_NO.getStatus());
				smanager.updateData(userStatus);
			}
		}
		
		
		
		
		/*List cfnlist = anconfig.getConfigsInfoByClass("上传材料名称(必需)");
		
		ZsFile file = new ZsFile();
		for (int i = 0; i < cfnlist.size(); i++) {
			AnConfig fn = (AnConfig) cfnlist.get(i);
			String fname = fn.getCoValue();
			file.setZsUser(user);
			file.setFiName(fname);
			file.setFiType("sbcl");
			List flist = fmanager.getDataByInstance(1, 1, null, null, file).getObjects();
			if (flist.size() > 0) {
				count += 1;
			}
		}
		ZsZxlb zxlb = new ZsZxlb();
		zxlb.setZsUser(user);
		List zlist = zmanager.getDataByUser(user);
		for (int i = 0; i < zlist.size(); i++) {
			ZsZxlb obj = (ZsZxlb) zlist.get(i);
			file.setZsUser(user);
			file.setFiName(obj.getZxId().toString());
			file.setFiType("zxlb");
			List flist = fmanager.getDataByInstance(1, 1, null, null, file).getObjects();
			if (flist.size() > 0) {
				count += 1;
			}
		}
		ZsShhd shhd = new ZsShhd();
		shhd.setZsUser(user);
		List hlist = hmanager.getDataByUser(user);
		for (int i = 0; i < hlist.size(); i++) {
			ZsShhd obj = (ZsShhd) hlist.get(i);
			file.setZsUser(user);
			file.setFiName(obj.getHdId().toString());
			file.setFiType("shhd");
			List flist = fmanager.getDataByInstance(1, 1, null, null, file).getObjects();
			if (flist.size() > 0) {
				count += 1;
			}
		}
		ZsHjqk hjqk = new ZsHjqk();
		hjqk.setZsUser(user);
		List jlist = jmanager.getDataByUser(user);
		for (int i = 0; i < jlist.size(); i++) {
			ZsHjqk obj = (ZsHjqk) jlist.get(i);
			file.setZsUser(user);
			file.setFiName(obj.getHjId().toString());
			file.setFiType("hjqk");
			List flist = fmanager.getDataByInstance(1, 1, null, null, file).getObjects();
			if (flist.size() > 0) {
				count += 1;
			}
		}

		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "+count+" " +(cfnlist.size()+zlist.size()+hlist.size()+jlist.size()));
		if (count >= (cfnlist.size()+zlist.size()+hlist.size()+jlist.size())) {
			if (!Status.FILESTATUS_YES.getStatus().equals(
					userStatus.getFileStatus())) {
				userStatus.setFileStatus(Status.FILESTATUS_YES.getStatus());
				smanager.updateData(userStatus);
			}
		} else if (count == 0) {
			if (!Status.FILESTATUS_NULL.getStatus().equals(
					userStatus.getFileStatus())) {
				userStatus.setFileStatus(Status.FILESTATUS_NULL.getStatus());
				smanager.updateData(userStatus);
			}
		} else {
			if (!Status.FILESTATUS_NO.getStatus().equals(
					userStatus.getFileStatus())) {
				userStatus.setFileStatus(Status.FILESTATUS_NO.getStatus());
				smanager.updateData(userStatus);
			}
		}*/
	}

}
